#include "Dictionary.h"

using namespace std;
  
void Dictionary::setData(const string& dictPath, const string& indexPath){
    ifstream file(dictPath);
    if (file.is_open()) {
        _dict.clear();
        string word;
        int frequency;
        while (file >> word >> frequency) {
            //cout << word << " " << frequency << endl;
            _dict.push_back(make_pair(word, frequency));
        }
        file.close();
    }
    else{
        cout <<"ifstream open file" <<dictPath <<"error" <<endl;
        return;
    }
    ifstream file2(indexPath);
    if (file2.is_open()) {
        string line;
        while (getline(file2, line)) {
            istringstream iss(line);
            string word;
            int lineNum;
            iss >> word;
            //cout << word << endl;
            while(iss >> lineNum){
                //cout << lineNum << endl;
                _indexTable[word].insert(lineNum);
            }
        }
        file2.close();
    }
    else{
        cout <<"ifstream open file" <<indexPath <<"error" <<endl;
        return;
    }
}
void Dictionary::setDictType(int val){
    _dictType = val;
}
int Dictionary::getDictType(){
    return _dictType;
}

vector<pair<string, int>>& Dictionary::getDict() {
    return _dict;
}

map<string, set<int>>& Dictionary::getIndexTable() {
    return _indexTable;
}

string Dictionary::doQuery(string word) {
    // Perform query logic here
    return "Query result";
}

Dictionary *Dictionary::_pInstance = nullptr;
pthread_once_t Dictionary::_once = PTHREAD_ONCE_INIT;
int Dictionary::_dictType = 0; 
